package app.goods.info.views.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;

import app.goods.info.services.CentralizedProcurementGoodsServie;
import app.goods.info.services.domain.CataLogue;
import app.goods.info.services.domain.GoodsInfo;
import app.goods.info.utils.InterfaceUtils;

public class RefreshActionPerformListenner implements ActionListener {
	private CataLogue cataLogue;
	private JFrame frame;
	CentralizedProcurementGoodsServie centralizedProcurementGoodsServie = new CentralizedProcurementGoodsServie();

	public RefreshActionPerformListenner(CataLogue cataLogue, JFrame frame) {
		super();
		this.cataLogue = cataLogue;
		this.frame = frame;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JLabel refreshStatusLabel = InterfaceUtils.getComponentByName(frame,
				"refresh-status-".concat(cataLogue.getCatalogueName()), JLabel.class);
		refreshStatusLabel.setText("正在下载数据...");
		JButton refreshButton = ((JButton) e.getSource());
		refreshButton.setEnabled(false);
		long now = System.currentTimeMillis();
		new Thread(() -> {
			// 创建SwingWorker实例
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				@Override
				protected Void doInBackground() throws Exception {
					// 根据分类重新下载每个分类对应的数据
//					List<GoodsInfo> goodsInfos = centralizedProcurementGoodsServie
//							.getCentralizedGoodsByCatalogueNo1(cataLogue);
					List<GoodsInfo> goodsInfos = centralizedProcurementGoodsServie
							.getCentralizedGoodsByCatalogueNoWithShopNo(cataLogue);
					JTable catalogueJTable = InterfaceUtils.findJTableByName(frame,
							"table-goods-list-".concat(cataLogue.getCatalogueName()));

					List<String> columnNames = new ArrayList<String>();
					columnNames.add("序号");
					columnNames.add("供应商名称");
					columnNames.add("商品名称");
					columnNames.add("销售单位");
					columnNames.add("商户指导价");
					columnNames.add("数量");

					Object[][] dataRows = new Object[goodsInfos.size()][6];
					for (int i = 0; i < goodsInfos.size(); i++) {
						GoodsInfo goodsInfo = goodsInfos.get(i);
						dataRows[i][0] = i;
						dataRows[i][1] = goodsInfo.getShopName();

						if ("正大翅中".equals(goodsInfo.getGoodsName()) || "正大翅根".equals(goodsInfo.getGoodsName())
								|| "益客翅根".equals(goodsInfo.getGoodsName())) {
							dataRows[i][2] = goodsInfo.getGoodsName() + "（" + goodsInfo.getGoodsUnit() + "）";
						} else if ("益客翅中".equals(goodsInfo.getGoodsName())) {
							dataRows[i][2] = "益客翅中（M号）240-260/箱";
						} else {
							dataRows[i][2] = goodsInfo.getGoodsName();
						}

						dataRows[i][3] = goodsInfo.getGoodsUnit();
						dataRows[i][4] = goodsInfo.getGoodsPrice();
						dataRows[i][5] = "0";
					}
					;

					SwingUtilities.invokeLater(() -> {
						DefaultTableModel model = (DefaultTableModel) catalogueJTable.getModel();
						model.setDataVector(dataRows, columnNames.toArray());
						model.fireTableDataChanged();
						catalogueJTable.getColumnModel().getColumn(0).setPreferredWidth(20);
						catalogueJTable.getColumnModel().getColumn(1).setPreferredWidth(150);
						catalogueJTable.getColumnModel().getColumn(2).setPreferredWidth(150);
						catalogueJTable.getColumnModel().getColumn(3).setPreferredWidth(30);
						catalogueJTable.getColumnModel().getColumn(4).setPreferredWidth(40);
						catalogueJTable.getColumnModel().getColumn(5).setPreferredWidth(30);
						catalogueJTable.updateUI();
//    					catalogueJTable.repaint();
						refreshButton.setEnabled(true);
						refreshStatusLabel.setText("下载完成，下载数据：" + goodsInfos.size() + "条记录，共耗时："
								+ ((System.currentTimeMillis() - now) / 1000) + "秒");
						InterfaceUtils.cacaulateTotalPrice(frame);
					});
					return null;

				}
			};
			// 执行SwingWorker
			worker.execute();
		}).start();
	}
}
